import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../components/Layout.vue'
import { useUserStore } from '../store/user'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    meta: { requiresAuth: true },
    children: [
      {
        path: 'home',
        name: 'Home',
        component: () => import('../views/Home.vue'),
        meta: { requiresAuth: true, title: '首页' }
      },
      {
        path: 'inquiry',
        name: 'Inquiry',
        component: () => import('../views/coze.vue'),
        meta: { requiresAuth: true, title: '皮肤问诊' }
      },
      {
        path: 'inquiry/history',
        name: 'InquiryHistory',
        component: () => import('../views/InquiryHistory.vue'),
        meta: { requiresAuth: true, title: '问诊历史' }
      },
      {
        path: 'inquiry/detail/:id',
        name: 'InquiryDetail',
        component: () => import('../views/InquiryDetail.vue'),
        meta: { requiresAuth: true, title: '问诊详情' }
      },
      {
        path: 'chronic',
        name: 'ChronicDisease',
        component: () => import('../views/ChronicDisease.vue'),
        meta: { requiresAuth: true, title: '皮肤档案' }
      },
      {
        path: 'chronic/history',
        name: 'ChronicDiseaseHistory',
        component: () => import('../views/ChronicDiseaseHistory.vue'),
        meta: { requiresAuth: true, title: '档案历史' }
      },
      {
        path: 'chronic/detail/:id',
        name: 'ChronicDiseaseDetail',
        component: () => import('../views/ChronicDiseaseDetail.vue'),
        meta: { requiresAuth: true, title: '档案详情' }
      },
      {
        path: 'diet',
        name: 'Diet',
        component: () => import('../views/DietManage.vue'),
        meta: { requiresAuth: true, title: '饮食建议' }
      },
      {
        path: 'diet/history',
        name: 'DietHistory',
        component: () => import('../views/DietHistory.vue'),
        meta: { requiresAuth: true, title: '饮食记录' }
      },
      {
        path: 'diet/detail/:id',
        name: 'DietDetail',
        component: () => import('../views/DietDetail.vue'),
        meta: { requiresAuth: true, title: '饮食详情' }
      },
      {
        path: 'exercise',
        name: 'Exercise',
        component: () => import('../views/Exercise.vue'),
        meta: { requiresAuth: true, title: '生活方式' }
      },
      {
        path: 'exercise/history',
        name: 'ExerciseHistory',
        component: () => import('../views/ExerciseHistory.vue'),
        meta: { requiresAuth: true, title: '生活记录' }
      },
      {
        path: 'exercise/detail/:id',
        name: 'ExerciseDetail',
        component: () => import('../views/ExerciseDetail.vue'),
        meta: { requiresAuth: true, title: '生活详情' }
      },
      {
        path: 'tcm-constitution',
        name: 'TcmConstitution',
        component: () => import('../views/TcmConstitution.vue'),
        meta: { requiresAuth: true, title: '皮肤类型' }
      },
      {
        path: 'tcm-constitution/history',
        name: 'TcmConstitutionHistory',
        component: () => import('../views/TcmConstitutionHistory.vue'),
        meta: { requiresAuth: true, title: '类型记录' }
      },
      {
        path: 'tcm-constitution/detail/:id',
        name: 'TcmConstitutionDetail',
        component: () => import('../views/TcmConstitutionDetail.vue'),
        meta: { requiresAuth: true, title: '类型详情' }
      },
      {
        path: 'tcm-encyclopedia',
        name: 'TcmEncyclopedia',
        component: () => import('../views/TcmEncyclopedia.vue'),
        meta: { requiresAuth: true, title: '皮肤百科' }
      },
      {
        path: 'tcm-encyclopedia/:id',
        name: 'TcmEncyclopediaDetail',
        component: () => import('../views/TcmEncyclopediaDetail.vue'),
        meta: { requiresAuth: true, title: '百科详情' }
      },
      {
        path: 'lecture',
        name: 'Lecture',
        component: () => import('../views/Lecture.vue'),
        meta: { requiresAuth: true, title: '皮肤讲堂' }
      },
      {
        path: 'lecture/:id',
        name: 'LectureDetail',
        component: () => import('../views/LectureDetail.vue'),
        meta: { requiresAuth: true, title: '讲堂详情' }
      },
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('../views/Profile.vue'),
        meta: { requiresAuth: true, title: '个人档案' }
      },
      {
        path: 'profile/edit',
        name: 'ProfileEdit',
        component: () => import('../views/ProfileEdit.vue'),
        meta: { requiresAuth: true, title: '编辑个人信息' }
      },
      {
        path: 'profile/change-password',
        name: 'ChangePassword',
        component: () => import('../views/ChangePassword.vue'),
        meta: { requiresAuth: true, title: '修改密码' }
      },
      {
        path: 'healthcheck',
        name: 'HealthCheck',
        component: () => import('../views/HealthCheck.vue'),
        meta: { requiresAuth: true, title: '皮肤检测' }
      },
      {
        path: 'healthcheck/history',
        name: 'HealthCheckHistory',
        component: () => import('../views/HealthCheckHistory.vue'),
        meta: { requiresAuth: true, title: '检测历史' }
      },
      {
        path: 'healthcheck/detail/:id',
        name: 'HealthCheckDetail',
        component: () => import('../views/HealthCheckDetail.vue'),
        meta: { requiresAuth: true, title: '检测详情' }
      },
      {
        path: 'health-record',
        name: 'HealthRecord',
        component: () => import('../views/HealthRecord.vue')
      },
      {
        path: 'profile-skin',
        name: 'ProfileSkin',
        component: () => import('../views/ProfileSkin.vue'),
        meta: { requiresAuth: true, title: '皮肤档案' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()

  if (to.meta.requiresAuth && !userStore.token) {
    next('/login')
  } else if ((to.path === '/login' || to.path === '/register') && userStore.token) {
    next('/')
  } else {
    next()
  }
})

export default router 